package com.wx.mall.goods.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.PageVo;
import com.wx.mall.common.exceptions.ApiException;
import com.wx.mall.common.vo.goods.SkuVo;
import com.wx.mall.goods.service.ISkuService;
import com.wx.mall.goods.vo.req.SkuMatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljl
 * @since 2020-04-16
 */
@RestController
@RequestMapping("/sku")
@Api(tags = "商品SKU相关接口")
public class SkuController {
    @Autowired
    private ISkuService service;

    @GetMapping("/pageList")
    @ApiOperation("分页获取商品SKU数据")
    public CommonResult pageList(@ApiParam("分页vo") @Valid PageVo vo) {
        CommonPage<SkuVo> list = service.pageList(vo);
        return CommonResult.success(list);
    }

    @GetMapping("/pageList/{id}")
    @ApiOperation("通过SKU ID 分页获取商品SKU数据")
    public CommonResult pageListById(@ApiParam("分页vo") @Valid PageVo vo, @ApiParam("商品SKU id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        return CommonResult.success(service.pageListBySpuId(vo,id));
    }

    @GetMapping("/list/{id}")
    @ApiOperation("通过SKU ID 获取商品SKU数据")
    public CommonResult listById(@ApiParam("商品SKU id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        return CommonResult.success(service.listBySpuId(id));
    }

    @GetMapping("/pageMatch")
    @ApiOperation("通过SKU encode 分页获取商品SKU数据")
    public CommonResult pageMatch(@ApiParam("分页vo") @Valid SkuMatchVo vo) {
        return CommonResult.success(service.pageMatch(vo));
    }

    @GetMapping("/match")
    @ApiOperation("通过SKU encode 获取商品SKU数据")
    public CommonResult match(@ApiParam("商品SKU encode") String encode) {
        return CommonResult.success(service.selectMatch(encode));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除品牌数据")
    public CommonResult deleteEntity(@ApiParam("SKU id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        service.deleteEntity(id);
        return CommonResult.success();
    }

    @PostMapping("/addSku")
    @ApiOperation("新增SKU信息 并返回其ID")
    public CommonResult add(@Valid SkuVo vo) {
        service.insertEntity(vo);
        return CommonResult.success();
    }

    @PutMapping("/updateSku")
    @ApiOperation("根据id更新品牌数据")
    public CommonResult update(@Valid SkuVo vo) {
        return CommonResult.success(service.updateEntity(vo));
    }

    // ElementUI el-upload 带参数上传文件 后台接受方式
    @PostMapping("/uploadImage")
    @ApiOperation("商品SKU图片上传")
    public CommonResult uploadImage(@ApiParam("image文件") @RequestParam("files") MultipartFile[] files, @ApiParam("其他formData参数") @RequestParam Map<String,String> param) {
        //获取http服务访问端口
//        String logoUrlPrefix = req.getRequestURL().toString().replace(req.getRequestURI(), "");

        String uuid = "";
        if (param.get("skuId") != null){
            uuid = param.get("skuId").trim();
        }
        List<Map<String, String>> list = new ArrayList<>();

        // 文件列表是否为空
        if (files.length > 0){
            for (MultipartFile file:files) {
                String name = file.getOriginalFilename();
                if (uuid != null && !uuid.equals("")){
                    name = "skuId=" + uuid + "&fileName=" + name;
                }
                list.add(service.uploadImage(file, name));
            }
        }else {
            throw new ApiException("上传的文件列表为空！");
        }
        return CommonResult.success(list);
    }

    @DeleteMapping("/deleteImage")
    @ApiOperation("商品SKU图片上传")
    public CommonResult deleteImage(@ApiParam("image名称") @RequestParam("imgName") String imgName, @ApiParam("sku编号") @RequestParam("skuId") String skuId) {
        //获取http服务访问端口
//        String logoUrlPrefix = req.getRequestURL().toString().replace(req.getRequestURI(), "");
        String fileName = "skuId=" + skuId + "&fileName=" + imgName;
        return CommonResult.success(service.deleteImage(fileName));
    }

}

